package com.app.manage.moudles.confs.service.impl;

import com.app.manage.moudles.confs.dao.TGroupRelationDao;
import com.app.manage.moudles.confs.entity.TGroupRelation;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.app.manage.moudles.confs.dao.TGroupDao;
import com.app.manage.moudles.confs.entity.TGroup;
import com.app.manage.moudles.confs.service.TGroupService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 组别(TGroup)表服务实现类
 *
 * @author makejava
 * @since 2025-05-16 11:17:34
 */
@Service("tGroupService")
public class TGroupServiceImpl extends ServiceImpl<TGroupDao, TGroup> implements TGroupService {

    @Resource
    private TGroupRelationDao tGroupRelationDao;


    @Override
    public String saveData(TGroup tGroup) {
        long count = count(new LambdaQueryWrapper<TGroup>()
                .ne(tGroup.getId() != null,  TGroup::getId,tGroup.getId())
                .eq(TGroup::getGroupCode,tGroup.getGroupCode())
                .last(" limit 1")
        );
        if(count > 0){
            return "当前组别编码已经存在！";
        }
        if(tGroup.getId() == null){
            tGroup.setCreateTime(new Date());
            tGroup.setUpdateTime(new Date());
            save(tGroup);
        }else {
            tGroup.setUpdateTime(new Date());
            updateById(tGroup);
        }


        return null;
    }

    @Override
    public <T> String removeData(List<T> idList) {
        //查询是否有存在的
        long count = tGroupRelationDao.selectCount(
                new LambdaQueryWrapper<TGroupRelation>()
                .in(TGroupRelation::getGroupId,idList)
                .last("limit 1")
        );
        if(count > 0){
            return "当前分组已绑定，无法删除";
        }
        removeBatchByIds(idList);

        return null;
    }
}

